﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Domain.IServices;
using VOL.Domain.IServices.MasterData;
using VOL.Entity.DomainModels;

namespace VOL.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MasterDataController : ControllerBase
    {
        /// <summary>
        /// 供应商管理服务
        /// </summary>
        private ISupplierServices _supplierServices;
        private readonly IMaterialCatalogService _materialCatalogService;

        public MasterDataController(ISupplierServices supplierServices,IMaterialCatalogService materialCatalogService)
        {
            _supplierServices = supplierServices;
            this._materialCatalogService = materialCatalogService;
        }


        /// <summary>
        /// 添加供应商管理
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSupplier([FromBody]Supplier supplier)
        {
            var res= await _supplierServices.AddSupplier(supplier);
            return res;
        }
        /// <summary>
        /// 供应商管理的显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Supplier>> GetAllSupplierList()
        {
           var res= await _supplierServices.GetAllSupplierList();
            return res;
        }
        /// <summary>
        /// 查询物料产品分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTree()
        {
            var tree = await _materialCatalogService.GetMaterialCatalogTreeAsync();
            return Ok(tree);
        }
        /// <summary>
        /// 添加修改物料产品分类
        /// </summary>
        /// <param name="materialCatalog"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<WebResponseContent> CreateUpdateMaterialCatalog([FromBody]MaterialCatalog materialCatalog)
        {
            return _materialCatalogService.CreateUpdateMaterialCatalog(materialCatalog);
        }
        /// <summary>
        /// 删除物料产品分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteMaterialCatalog(int id)
        {
            var result = await _materialCatalogService.DeleteMaterialCatalogAsync(id);

            if (result)
                return Ok(new { success = true, message = "删除成功" });
            else
                return BadRequest(new { success = false, message = "删除失败" });
        }
    }
}
